package addition_fr;

import java.util.*;

public class Questionnaire {

    protected Set<IQuestion> questionEtReponsesPossibles = null;

    public Questionnaire() {
        questionEtReponsesPossibles = new HashSet<IQuestion>();
    }

    public void ajouteQuestionReponseScore(int numQuestion, String questionField, String reponse, int score) {
        IQuestion question = findQuestion(numQuestion);
        if (null == question) {
            question = new Question(numQuestion, questionField);
            questionEtReponsesPossibles.add(question);
        }

        question.addReponseScore(reponse, score);
    }

    public void ajouteQuestionReponseScoreAdditionnelle(int numQuestion, String questionAdditionnelle, String reponse, int score) {
        IQuestion question = findQuestion(numQuestion);
        assert question != null : "BUG: Il n'existe pas de question " + Integer.valueOf(numQuestion);
        if (question != null) {
            question.addReponseScoreAdditionnelle(questionAdditionnelle, reponse, score);
            questionEtReponsesPossibles.add(question);
        } else {
            System.out.println("BUG: Il n'existe pas de question " + Integer.valueOf(numQuestion));
        }
    }

    public IQuestion findQuestion(int numQuestion) {
        Iterator<IQuestion> itQuestionEtReponsesPossibles = questionEtReponsesPossibles.iterator();
        IQuestion currentQuestion = null;
        while (itQuestionEtReponsesPossibles.hasNext()) {
            currentQuestion = itQuestionEtReponsesPossibles.next();
            if (currentQuestion.getNumQuestion() == numQuestion) {
                return currentQuestion;
            }
        }
        return null;
    }

    IReponseScore findReponseScore(int numQuestion, String reponse) {
        IQuestion question = findQuestion(numQuestion);
        assert question != null : "ERROR: Il n'existe pas de question " + Integer.valueOf(numQuestion);
        if (null != question) {
            return question.find(reponse);
        } else {
            System.out.println("ERROR: Il n'existe pas de question " + Integer.valueOf(numQuestion));
            return null;
        }
    }
}
